<?php
namespace app\admin\controller;

use app\admin\model\SortModel;

 use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
class Manager extends Common
{


    public function index()
    {
        $db=Db::name("manager");
        $list = Db::name('manager')-> paginate(10);
        $this->assign("list",$list);


        return $this->fetch("index");
    }


    public function person()
    {
        $uid=session('uid');
        $v = Db::name('manager')->find($uid);

        if(Request::instance()->isPost()){
            echo $uid;



            print_r($_POST);
            $data['realname']=$_POST['realname'];
            $data['username']=$_POST['username'];
            $data['password']=md5($_POST['pass']);
            $oldpassword=md5($_POST['oldpass']);
            if($v['password']==$oldpassword){
                if(Db::name("manager")->where("id=".$uid)->update($data)){
                    $this->success("修改成功","/admin/manager/person.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                $this->error("原密码错误");
            }


        }
//        print_r($v);
        $this->assign("v",$v);


        return $this->fetch("person");
    }


    public function add()
    {
        $id=input('id',0);
        $db=Db::name("manager");
        $v=$db->find($id);
//        print_r($_POST);
        if($v){
        }else{
            $v['id']='';
            $v['realname']="";
            $v['username']="";
            $v['role_name']="";
            $v['role_id']="";
            $v['status']=1;
        }


        if(Request::instance()->isPost()){
            $data['realname']=$_POST['realname'];
            $data['username']=$_POST['username'];
            $data['role_id']=$_POST['role_id'];
            $data['password']=md5($_POST['password']);
            $data['status']=1;


            if($id){
                // print_r($data);
                // echo $id;
//                die;
                if(Db::name("manager")->where("id=".$id)->update($data)){
                    $this->success("修改成功","/admin/manager/index.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                $data['sendtime']=time();
                if(Db::name("manager")->insert($data)){
                    $this->success("添加成功","/admin/manager/index.html");
                }else{
                    $this->error("添加失败");
                }
            }

        }



        $this->assign("v",$v);
        return $this->fetch("add");
    }


    public function del()
    {
        $id=input('id',0);
        if(Db::name("manager")->delete($id)){
            $this->success("删除成功","/admin/manager/index.html");
        }else{
            $this->error("删除失败");
        }



    }

    public function audit()
    {

        $id=input("id",0);
//        echo $id;

        $v=Db::name("manager")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("manager")->where("id=".$id)->update($data)){
            $this->success("审核成功","/admin/manager/index.html");
//            $return_data['status'] = 0;
//            $return_data['msg'] ="审核成功";
        }else{
            $this->error("审核失败");
//            $return_data['status'] = 1;
//            $return_data['msg'] ="审核失败";
        }

//        echo json_encode($return_data);


    }
}